package com.it;

import java.util.Random;
import java.util.Scanner;

public class Guess {
    public static void main(String[] args) {
        //猜随机数
        int i = new Random().nextInt(1000) + 1;//确认随机数的范围1-1000，并赋值给i
        System.out.println(i);//作弊
        guessingNumbers(i);

    }

    //猜数字的方法,int i 参数为随机数
    public static void guessingNumbers(int i) {
        int count = 0;//默认猜的次数
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入你要猜的数字");
            int num = scanner.nextInt();//输入要猜的数字
            if (num > i) {
                count++;
                System.out.println("猜大了,已经猜了" + count + "次");
            } else if (num < i) {
                count++;
                System.out.println("猜小了" + count + "次");
            } else {
                count++;
                System.out.println("恭喜你猜对了，用了" + count + "次");
                break;
            }
        }
    }
}
